package com.example.androidlearn.notification;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.androidlearn.R;

public class NotificationActivity extends AppCompatActivity {

    String strChannelID = "strChannelID";
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        Button button = (Button) findViewById(R.id.notification);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // name属性可以在应用程序通知中看到，属于类别
        NotificationChannel channel = new NotificationChannel(strChannelID, "不知道这是个啥", NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    Intent intent = new Intent(NotificationActivity.this, NotifucationIndentActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);

                    Notification.Builder builder = new Notification.Builder(NotificationActivity.this, strChannelID);
                    builder.setSmallIcon(R.mipmap.ic_launcher);
                    builder.setTicker("这个Ticker是通知的标题吗？");
                    builder.setContentTitle("这个是ContentTitle:editText.getText().toString()" );
                    builder.setContentText("这个是ContentText:editText1.getText().toString()" );
                    builder.setContentIntent(pendingIntent);

                    manager.notify(0x01, builder.build());
                } else {
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this);
                    builder.setSmallIcon(R.mipmap.ic_launcher);
                    builder.setContentTitle("这个是ContentTitle:editText.getText().toString()" );
                    builder.setContentText("这个是ContentText:editText1.getText().toString()" );
                    builder.setOngoing(true);
                    manager.notify(0x02, builder.build());
                }
            }
        });
    }


}